package com.hlw.controller;


import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hlw.core.TreeTableModel;
import com.hlw.dao.RoleMapper;
import com.hlw.dao.UserMapper;
import com.hlw.entity.Menu;
import com.hlw.entity.Role;
import com.hlw.entity.User;
import com.hlw.service.IMenuService;
import com.hlw.service.IRoleService;
import com.hlw.service.IUserService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-05-17
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserMapper userMapper;
	@Resource
	private IUserService iUserService;
	@Resource
	private IRoleService roleService;
	@Resource
	private RoleMapper roleMapper;
	
	@RequestMapping("/userList")
	public String userList(Model model){
		//获取当前用户，只有角色为admin的才有权限曾删改查
	     User user = (User) SecurityUtils.getSubject().getPrincipal();
	    Integer rid= roleMapper.findRoleByUid(user.getId());
		Role role = roleService.selectById(rid);
		model.addAttribute("userRole", role.getName());
		return "/system/user/userlist";
		
	}
	@RequestMapping("/addUser")
	public String addUser(Model model){
		return "/system/user/user_add";		
	}
	@RequestMapping(value="/updateUser",method=RequestMethod.GET)
	public String updateUser(Model model,Integer id){
		User user =iUserService.selectById(id);
		model.addAttribute("user", user);
		return "/system/user/user_update";		
	}
	/**
	 * 删除用户
	 * @param model
	 * @param uid
	 * @return
	 */
	@RequestMapping("/delUser")
	@ResponseBody
	public String delUser(Integer id){
		User user =iUserService.selectById(id);
		if(user.getLoginname().equals("admin")){
			
			return "该账户为默认账户,不允许删除";
		}else{
			//查询删除账号是否存在角色表里，存在即删除
			Integer rid = roleService.findRoleByUid(id);
			if(rid!=null){
				roleService.deleteRoleByUid(id);
			}
			
			iUserService.deleteById(id);
			
			return "success";		
		}
		
		
	}
	
	@RequestMapping(value="/getAll")
	@ResponseBody
	public TreeTableModel findAllUser(){
		List<User> user =iUserService.findAll();
		System.out.println("所拿到的用户列表信息为："+user);
		return  new TreeTableModel(user);
	}
	
	/**
	 * 保存用户
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="saveUser",method=RequestMethod.POST)
	@ResponseBody
	public String innserMenu(@ModelAttribute("user")User user) {
		if(user.getId()==null){
			User nUser =iUserService.findUserByloginName(user.getLoginname());
			if(nUser!=null){
				return "该登陆名已存在，请重新输入！";
				
			}else{
				userMapper.insert(user);
				//新用户添加时角色默认为普通账户
				roleService.inserRoleByRidOrUid(user.getId(), 2);
				return "success";
			}
			
		}else{
			userMapper.updateAllColumnById(user);
			return "success";
			
		}
		 		
	}
	
	/**
	 * 更改用户状态
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="updateStatus",method=RequestMethod.POST)
	@ResponseBody
	public String updateStatus(@Param(value = "id")Integer id,@Param(value = "status")Integer status) {		
		Integer user = iUserService.updateStatus(id, status);
		System.out.println(user);
		return "success";
				 		
	}

	/**
	 * 修改用户密码页面
	 * @return
	 */
	@RequestMapping("/updateUsePwd")
	public String updateUsePwdList(Model model){
	     User user = (User) SecurityUtils.getSubject().getPrincipal();
	     model.addAttribute("uid", user.getId());
		return "/system/user/updatePwd";
		
	}
	/**
	 * 修改用户密码
	 * @return
	 */
	@RequestMapping("/updatePwd")
	@ResponseBody
	public String updateUsePwd(String npw,String psw){
	     User user = (User) SecurityUtils.getSubject().getPrincipal();
	     String tip=iUserService.loginNameAndPwd(user.getLoginname(), psw, npw);
		return tip;
		
	}
	
	
}
